﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Components.Modules;

using NCS.Library;
using NCS.Library.Components.Security;
using NCS.Library.Components.Users;
using NCS.Library.Components.Tabs;

using NCS.Library.Utilities;
using System.Text;
using NCS.Module.CMS.Library;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class Login : CMSWebControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public static void SystemErrorAlert(Page p, int intRetVal, string strErrMsg, bool Reload)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<script type=\"text/javascript\">");
            sb.Append("alert(\"" + strErrMsg + "\");");
            // sb.Append("\nif (opener) self.close();");

            if (Reload == true)
                sb.Append("history.go(-1);");

            sb.Append("</script>");

            p.ClientScript.RegisterClientScriptBlock(p.GetType(), "SystemErrorAlert", sb.ToString());
        }

        protected void btLogin_Click(object sender, ImageClickEventArgs e)
        {
            
            if ((UserController.ValidateUser(txtUsername.Text.Trim(), SecurityMethod.MD5Encrypt(txtPassword.Text.Trim())) != null))
            {
                AuthenticateUtility.LoginUser(txtUsername.Text.Trim(), true);
                if (Session["UrlKey"] != null)
                    Response.Redirect(Session["UrlKey"].ToString());
                else
                    Response.Redirect("/", true);
            }
            else
            {
                SystemErrorAlert(this.Page, 0, "Bạn đăng nhập sai hoặc chưa cấp quyền đăng nhập", true);
            }
        }

    }
}